/*
 * Author: Qiao Yu
 * Date: 2011.3.31
 */
package sjtu.edu.cn.se.compiler11.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;

import SyntaxTree.*;


public class SyntaxTree {
	private Tree theTree;
	private TreeItem root;
	private SimpleNode rootNode;
	private Shell theShell;
	
	public SyntaxTree (Composite parent, int style, Shell theShell) 
	{
		theTree = new Tree(parent, style);
		theTree.setSize(288,539);
	    
		root = new TreeItem(theTree,0);
		root.setText("SyntaxTree");
		root.setImage(new Image(theShell.getDisplay(), ".\\resources\\constructor.gif"));
		this.theShell = theShell;
			
	}
	
	public void setRootNode(Node value)
	{
		rootNode = (SimpleNode)value;
	}
	
	public void generateTree()
	{
		dump(rootNode,root);
	}
	
	public void clear()
	{
		theTree.removeAll();
		root = new TreeItem(theTree,0);
		root.setText("SyntaxTree");
		root.setImage(new Image(theShell.getDisplay(), ".\\resources\\constructor.gif"));

	}
	
	private void dump(SimpleNode theNode, TreeItem father)
	{
		Node[] children;
		children = theNode.getChildren();
		
		if (children != null) {
		      for (int i = 0; i < children.length; ++i) {
		        SimpleNode n = (SimpleNode)children[i];
		        if (n != null) {
		        	TreeItem newItem = new TreeItem(father,0);
		        	String text = n.toString();
		        	newItem.setText(text);
		        	
		        	if (text.compareTo("Expression") == 0)
		        		newItem.setImage(new Image(theShell.getDisplay(), ".\\resources\\expression_obj.gif"));
		        	else if (text.compareTo("StateMent") == 0)
		        		newItem.setImage(new Image(theShell.getDisplay(), ".\\resources\\statement.gif"));
		        	else if (text.compareTo("MainClass") == 0)
		        		newItem.setImage(new Image(theShell.getDisplay(), ".\\resources\\main.gif"));
		        	else if (text.compareTo("Type") == 0)
		         		newItem.setImage(new Image(theShell.getDisplay(), ".\\resources\\type.gif"));		
		        	else if (text.lastIndexOf("Exp") != -1)
		         		newItem.setImage(new Image(theShell.getDisplay(), ".\\resources\\expression.gif"));
		        	else if (text.lastIndexOf("Decl") != -1)
		         		newItem.setImage(new Image(theShell.getDisplay(), ".\\resources\\decl.gif"));
		        	else if (text.lastIndexOf("Statement") != -1)
		         		newItem.setImage(new Image(theShell.getDisplay(), ".\\resources\\stat.gif"));
		        	else if (text.indexOf("Identifier") != -1)
		         		newItem.setImage(new Image(theShell.getDisplay(), ".\\resources\\id.gif"));
		        	else if (text.indexOf("Op") != -1)
		         		newItem.setImage(new Image(theShell.getDisplay(), ".\\resources\\op.gif"));
		        	else if (text.indexOf("Symbol") != -1)
		         		newItem.setImage(new Image(theShell.getDisplay(), ".\\resources\\symbol.gif"));
		        	else if (text.lastIndexOf("List") != -1)
		         		newItem.setImage(new Image(theShell.getDisplay(), ".\\resources\\list.gif"));
		          	

		        	dump(n,newItem);
		        }
		      }
		    }
	}
}
